<template>
  <div class="embedding-ai-message farm-content" v-if="aiBackMsg && isShowAibox">
    <span class="title"> 智能分析：</span>
    <div v-html="aiBackMsg"></div>
    <el-progress :percentage="50" v-if="aiMsgLoading" status="success" :indeterminate="true" />
  </div>
  <ai_msg ref="aiRef" :is-show-msg="false" :start-handle="() => { aiMsgLoading = true }"
    :end-handle="() => { aiMsgLoading = false }"></ai_msg>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import ai_msg from '../ai/ai_msg.vue';
const aiMsgLoading = ref(false);
const aiRef = ref();
import farmHtmlUtils from '@/hook/farmHtmlUtils';
import type { SendMsg } from '@/types/ai/SendMsg';
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
const aiBackMsg = ref('');
const isShowAibox = ref(false);

/**
 *生成ai分析
 * @param word 检索内容
 * @param queryModel  检索模式
 * @param resultJson  引用资料
 */
const generateAiAnalysis = (word: string, queryModel: string, resultJson: string) => {
  //console.log('生成AI分析', word, queryModel, resultJson);
  // console.log(1);
  if (queryModel == 'EMBEDDING') {
    //  console.log(2);
    if (aiRef.value.isTalking()) {
      // console.log(3);
      isShowAibox.value = true;
      aiRef.value.stopTalk();
    }
    // console.log(4);
    isShowAibox.value = true;
    aiBackMsg.value = '...';
    aiRef.value.stopTalk();
    const msg = {
      msg: "我在进行一次知识检索，检索词为{{{" + word + "}}},同时知识库中有些资料或许对这个问题有帮助",
      quotingText: farmHtmlUtils.getMarkDown(resultJson),
      msgType: "SEARCH_ANALYSIS",
    } as SendMsg;
    aiRef.value.senMsg(msg, (text: string, html: string) => {
      //console.log(text);
      aiBackMsg.value = html;
    });
  } else {
    aiBackMsg.value = '';
    isShowAibox.value = false;
    // aiRef.value.stopTalk();
    // aiRef.value.clearMsg();
  }
};
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
// 暴露方法给父组件
defineExpose({
  generateAiAnalysis,
});
</script>
<style scoped>
/* 样式可以根据需要添加 */
.embedding-ai-message {
  border: 1px dotted #888888;
  font-size: 14px;
  color: #999999;
  padding: 20px;
  border-radius: 8px;
  max-width: 700px;
  background-color: var(--el-color-success-light-9);
  line-height: 2em;

  img {
    width: 26px;
    height: 26px;
  }

  .title {
    color: var(--el-color-success);
    font-weight: 700;
  }
}
</style>
